package com.inei.reportedeverificacion.model.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.reportedeverificacion.model.connection.UtilDB;
import com.inei.reportedeverificacion.model.entity.Hogar;

public class HogarDao implements ICRUD<Hogar> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	// constructor
	public HogarDao(Context context) {
		this.context = context;
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public Hogar buscar(Hogar o) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Hogar> leerViv(String id) {
		List<Hogar> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Hogar where CLAVEVIV=?", new String[]{""+id});
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Hogar>();
			while (!cursor.isAfterLast()) {
				Hogar obj = new Hogar();
				obj.setCLAVEVIVHOGAR(cursor.getString(cursor
						.getColumnIndex("CLAVEVIVHOGAR")));
				obj.setCLAVEVIV(cursor.getString(cursor
						.getColumnIndex("CLAVEVIV")));
				obj.setIdgrupo(cursor.getString(cursor
						.getColumnIndex("idgrupo")));
				obj.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	@Override
	public List<Hogar> leer() {
		List<Hogar> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Hogar", null);
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Hogar>();
			while (!cursor.isAfterLast()) {
				Hogar obj = new Hogar();
				obj.setCLAVEVIVHOGAR(cursor.getString(cursor
						.getColumnIndex("CLAVEVIVHOGAR")));
				obj.setCLAVEVIV(cursor.getString(cursor
						.getColumnIndex("CLAVEVIV")));
				obj.setIdgrupo(cursor.getString(cursor
						.getColumnIndex("idgrupo")));
				obj.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	@Override
	public int crear(Hogar o) {
		db = util.getWritableDatabase();
		db.beginTransaction();
		ContentValues cv = new ContentValues();
		cv.put("CLAVEVIV", o.getCLAVEVIV());
		cv.put("CLAVEVIVHOGAR", o.getCLAVEVIVHOGAR());
		cv.put("HOGAR", o.getHOGAR());
		int id = (int)db.insert("Hogar", null, cv);
		if(id>0){
			db.setTransactionSuccessful();
		}
		Cerrar();
		return id;
	}

	@Override
	public int eliminar(Hogar o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(Hogar o) {
		// TODO Auto-generated method stub
		return 0;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		db.close();
	}
}
